#ifndef __LCD4P3_C
#define __LCD4P3_C

#include "LCD4P3.h"

//写寄存器
void display_write_register8(uint8_t addr,uint8_t value){
		DISPLAY_BUFFER[0] = FRAME_HEADER_FIRST;
		DISPLAY_BUFFER[1] = FRAME_HEADER_SECOND;
		DISPLAY_BUFFER[2] = 0x03;   //指令长度
		DISPLAY_BUFFER[3] = WRITE_REGISTER;
		DISPLAY_BUFFER[4] = addr;
		DISPLAY_BUFFER[5] = value;

		#if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,6);	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,6);	
		#endif
	
}

//写寄存器
void display_write_register16(uint8_t addr,uint16_t value){
		DISPLAY_BUFFER[0] = FRAME_HEADER_FIRST;
		DISPLAY_BUFFER[1] = FRAME_HEADER_SECOND;
		DISPLAY_BUFFER[2] = 0x04;   //指令长度
		DISPLAY_BUFFER[3] = WRITE_REGISTER;
		DISPLAY_BUFFER[4] = addr;
		DISPLAY_BUFFER[5] = value >> 8;
		DISPLAY_BUFFER[6] = value & 0xff;	
	
		#if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,7);	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,7);	
		#endif
}

//写变量存储器
void display_write_variate(uint16_t addr,uint16_t value){
		DISPLAY_BUFFER[0] = FRAME_HEADER_FIRST;
		DISPLAY_BUFFER[1] = FRAME_HEADER_SECOND;
		DISPLAY_BUFFER[2] = 0x05; //指令长度
		DISPLAY_BUFFER[3] = WRITE_VARIATE;
		DISPLAY_BUFFER[4] = addr >> 8;
		DISPLAY_BUFFER[5] = addr & 0xff;
		DISPLAY_BUFFER[6] = value >> 8;
		DISPLAY_BUFFER[7] = value & 0xff;	 
	
	  #if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,8);	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,8);	
		#endif
}

//写变量存储器
void display_write_arr_variate(uint16_t addr,int8_t *arrValue,uint8_t len){ 
	  uint8_t i = 0;
		DISPLAY_BUFFER[0] = FRAME_HEADER_FIRST; 
		DISPLAY_BUFFER[1] = FRAME_HEADER_SECOND; 
		DISPLAY_BUFFER[2] = 3 + len; //指令长度
		DISPLAY_BUFFER[3] = WRITE_VARIATE; 
		DISPLAY_BUFFER[4] = addr >> 8; 
		DISPLAY_BUFFER[5] = addr & 0xff; 	  
	  for(i = 0;i < len; i++){
			  // 高位在前:
			  DISPLAY_BUFFER[6 + i] = arrValue[i]; 
		}
		
	  #if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,6 + len); 	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,6 + len);	
		#endif
}

//写变量存储器
void display_write_rtc(uint16_t addrRTC){  
		DISPLAY_BUFFER[0]  = FRAME_HEADER_FIRST;  
		DISPLAY_BUFFER[1]  = FRAME_HEADER_SECOND; 
	  DISPLAY_BUFFER[2]  = 0x0A; 
		DISPLAY_BUFFER[3]  = WRITE_VARIATE; 
		DISPLAY_BUFFER[4]  = addrRTC >> 8; 
		DISPLAY_BUFFER[5]  = addrRTC & 0xff; 
	  DISPLAY_BUFFER[6]  = lcdParam.RTC_data[0]; 
	  DISPLAY_BUFFER[7]  = lcdParam.RTC_data[1]; 
	  DISPLAY_BUFFER[8]  = lcdParam.RTC_data[2]; 
	  DISPLAY_BUFFER[9]  = lcdParam.RTC_data[3]; 
	  DISPLAY_BUFFER[10] = lcdParam.RTC_data[4]; 
	  DISPLAY_BUFFER[11] = lcdParam.RTC_data[5]; 
	  DISPLAY_BUFFER[12] = lcdParam.RTC_data[6]; 
	
	  #if uart_DMA_send > 0
		uart_DMA_sendDatas(DISPLAY_DMA_Chn,DISPLAY_DMA_flg_TC,DISPLAY_BUFFER,13);	
		#else
		uart_sendDatas(DISPLAY_USART,DISPLAY_BUFFER,13);	
		#endif
}

// 切换页面：
void change_page(uint16_t pageId){
	 display_write_register16(PIC_ID,pageId);
	 lcdParam.timer_recoder[0]  = 0;
	 lcdParam.timer_recoder[1]  = 0;
	 lcdParam.timer_recoder[2]  = 0;
	 lcdParam.lcd_page_id = (uint8_t)pageId;	 
}

// 获取错误对应的序号
uint8_t get_fault_idx(){  
    uint8_t showIdx = 0;  
	  switch(faultCode){
		case cp_fault:          //1
				showIdx = 1;  
				break;
		case low_voltage:       //2
				showIdx = 2;  
				break;
		case over_voltage:      //3
				showIdx = 3;  
				break;
		case PE_protection:     //4
				showIdx = 4;  
				break;
		case over_current:      //5
				showIdx = 5;  
				break;
		case short_circuit:     //6
				showIdx = 6;  
				break;
		case leakage:
				showIdx = 7;        //7
				break;
		case over_tempr:        //8
				showIdx = 8;  
				break;
		#ifdef SQDri_EVSEB_7KW
		case Istate_adjust:     //9
				showIdx = 9;  
				break;
		case carD1_bad:         //10
				showIdx = 13;  
				break;
		case relay_adhesion:    //11
				showIdx = 10;  
				break;
		case tmpr_boardIn_e:    //12
				showIdx = 8;  
				break;
		case I_data_e:          //13
				showIdx = 9;  
				break;
		case U_data_e:          //14
				showIdx = 9;  
				break;
		case tmpr_adjust_err:   //15
				showIdx = 9;  
				break;
		case flash_err:         //16
				showIdx = 9;  
				break;
		#endif
		default:                // 其他
				showIdx = 9;
		}	
		return showIdx;
}

// 刷新页面
void update_page(){
	  #if RTC_ON > 0 
		getTimestampRTC(); 
		#endif
    if(lcdParam.lcd_show_popup > 0 && lcdParam.lcd_popup_time > 0){
			  switch(lcdParam.lcd_show_popup){
				case lcd_popup_reservation_set:
					  // 弹出预约充电页面
				    
				    break;
			  case lcd_popup_delay_stop:
					  // 按键结束
					  if(lcdParam.lcd_page_id != (uint8_t)PageId_CHARGE_SUSPEND){
							  #if SEND_RTC_EN > 0
							  lcdParam.lcd_pageStart_tm = system_time;
							  #endif
								change_page(PageId_CHARGE_SUSPEND);
						}	
						break;
				}	
			  return;
		}
		switch(lcdParam.lcd_show_step){	
    #ifdef SQDri_EVSEB_7KW			
		case lcd_step_self_check:
			  // 自检界面
			  if(lcdParam.lcd_page_id != (uint8_t)PageId_SELF_TEST){
					  #if SEND_RTC_EN > 0
						lcdParam.lcd_pageStart_tm = system_time;
						#endif
						change_page(PageId_SELF_TEST);
				    lcdParam.countdownStart[0] = 3;
				}	
		    break;
		#endif		
	  case lcd_step_wait_plug:
			  // 等待插枪页面
			  if(lcdParam.lcd_page_id != (uint8_t)PageId_WAIT_PLUG){
					  #if SEND_RTC_EN > 0
						lcdParam.lcd_pageStart_tm = system_time;
						#endif
						change_page(PageId_WAIT_PLUG);
				}	
		  	break;		
		#if card_type_sel > 0		
	  case lcd_step_card_startup:
			  // 等待刷卡页面
			  if(lcdParam.lcd_page_id != (uint8_t)PageId_SWIPING_CARD){
					  #if SEND_RTC_EN > 0
						lcdParam.lcd_pageStart_tm = system_time;
						#endif
						change_page(PageId_SWIPING_CARD);
				}	
		  	break;
		#endif
		#if Non_Plug_And_Charge > 0
		case lcd_step_bottn_startup:
			  // 按钮启动页面
		    
		  	break;
		case lcd_step_app_startup:
				// app启动页面
				break;
		#endif
		#if RESV_CHG_ON > 0
		case lcd_step_app_reservation:
				// APP预约页面
		  	break;
	  #endif
		#ifdef SQDri_EVSEB_7KW
		case lcd_step_safe_check:
			  //安全检查页面
			  if(lcdParam.lcd_page_id != (uint8_t)PageId_SECURITY_DETECTION){
					  #if SEND_RTC_EN > 0
						lcdParam.lcd_pageStart_tm = system_time;
						#endif
						change_page(PageId_SECURITY_DETECTION);
				}		
		  	break;
		#endif		
		case lcd_step_wait_chg:
			  // 等待充电页面
			  if(lcdParam.lcd_page_id != (uint8_t)PageId_WAIT_CHG){					  
					  #if SEND_RTC_EN > 0
						lcdParam.lcd_pageStart_tm = system_time;
						#endif
						change_page(PageId_WAIT_CHG);
				}	
			  break;	
		case lcd_step_chging:
			  // 正在充电页面
			  if(lcdParam.lcd_page_id != (uint8_t)PageId_CHGING){					  
					  #if SEND_RTC_EN > 0
						lcdParam.lcd_pageStart_tm = system_time;
						#endif
						change_page(PageId_CHGING);
				}	
			  break;
		case lcd_step_EV_pause:
			  // 车辆暂停充电页面
			  if(lcdParam.lcd_page_id != (uint8_t)PageId_CHARGE_SUSPEND){
					  #if SEND_RTC_EN > 0
						lcdParam.lcd_pageStart_tm = system_time;
						#endif
						change_page(PageId_CHARGE_SUSPEND);
				}	
			  break;
		case lcd_step_chg_full:
			  // 充满电页面
			  if(lcdParam.lcd_page_id != (uint8_t)PageId_CHARGE_FULL){
					   #if SEND_RTC_EN > 0
						 lcdParam.lcd_pageStart_tm = system_time;
						 #endif
   				   change_page(PageId_CHARGE_FULL);
    		} 
			  break;
		case lcd_step_plug_out:
			  // 等待拔枪页面
				if(lcdParam.lcd_page_id != (uint8_t)PageId_Wait_PLUGOUT){
					   #if SEND_RTC_EN > 0
						 lcdParam.lcd_pageStart_tm = system_time;
						 #endif
   				   change_page(PageId_Wait_PLUGOUT);
    		} 
				break;
		case lcd_step_chg_error: 
        // 出现异常页面        
        if(system_time - lcdParam.lcd_showErr_tm > 50000){            					
					  lcdParam.lcd_showErr_tm = system_time; // 刷新下时间
				}	
				lcdParam.fualtIdOffst  = get_fault_idx() - 1;
        if(system_time - lcdParam.lcd_showErr_tm < 6000){
					  if(lcdParam.lcd_page_id != (uint8_t)(PageId_first_error_page + lcdParam.fualtIdOffst)){
								change_page(PageId_first_error_page + lcdParam.fualtIdOffst);					  
						}	
				} else {
				    if(lcdParam.lcd_page_id != (uint8_t)(PageId_first_error_detail + lcdParam.fualtIdOffst)){
								change_page(PageId_first_error_detail + lcdParam.fualtIdOffst);
						}	
				}	
			  break;		
    }		
}

// 调整屏幕亮度
void lcd_brightness_set(uint8_t brightnessValue){
	  // 设置亮度未改变，不进行亮度设置
	  if(brightnessValue == lcdParam.lcd_luminance)  return;  
	  if(brightnessValue > 0x40){
			  brightnessValue = 0x40;
		}
	  display_write_register8(addr_brightness,brightnessValue);
    lcdParam.lcd_luminance = 	brightnessValue;	
}

#if beep_type == beep_LCD 
// 蜂鸣器鸣叫
void lcd_beep(uint8_t beepTm){
	 display_write_register8(addr_beep,beepTm);	
}

// 蜂鸣器鸣叫：beep_times_input = 鸣叫次数; beep_Tm_input = 鸣叫坚持时间；beep_idle_Tm_input = 每次鸣叫后时间间隔
void beep_start(uint8_t beep_times_input,uint16_t beep_Tm_input,uint16_t beep_idle_Tm_input){
	  #if RTC_ON > 0 
		getTimestampRTC(); 
		#endif
	  beep_start_tm = system_time;
	  beep_step = 1;
	  beep_times = beep_times_input;
	  beep_Tm = beep_Tm_input;
	  beep_idle_Tm = beep_idle_Tm_input;	
}
void lcd_beep_step_run(){
	  #if RTC_ON > 0 
		getTimestampRTC(); 
		#endif
	  if(system_time - beep_start_tm > beep_Tm + beep_idle_Tm){
				lcd_beep((uint16_t)(beep_Tm / 10));
			  beep_start_tm = system_time;
				beep_step++;
		}
}
#endif



#endif

